package com.pactera.demo.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author chang.zhou
 * @version version
 * @date Created in 2020/3/11 14:47
 */
@Slf4j
@Controller
public class Client01Controller {

    @GetMapping
    public String index(){
        return "index";
    }

    @GetMapping("/sign-on")
    public String index1(){
        return "redirect:/";
    }

    @GetMapping("/user-details")
    @ResponseBody
    public Object userDetails(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info("authentication->{}", authentication);
        Map<String,String> map = new HashMap<>();
        if(authentication != null  && authentication.getPrincipal() != null
                && authentication.getPrincipal() instanceof UserDetails){
            UserDetails user = (UserDetails) authentication.getPrincipal();
            map.put("username", user.getUsername());
            log.info("user->{}",user );
        }

        return map;
    }

}
